package com.xinongbest.utils;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author lemonwan
 * 数据库连接工具类：保证一个线程始终是使用一个连接，单例
 */
public class ConnectionUtils {

    /**
     * 把数据库连接和线程绑定
     */
    private ThreadLocal<Connection> connectionThreadLocal = new ThreadLocal<>();

//    private static ConnectionUtils connectionUtils = new ConnectionUtils();
//
//    private ConnectionUtils() {
//
//    }
//
//    public static ConnectionUtils getInstance() {
//        return connectionUtils;
//    }

    public ConnectionUtils() {

    }

    /**
     * 获取数据库连接
     *
     * @return
     * @throws SQLException
     */
    public Connection getConnect() throws SQLException {
        Connection connect = connectionThreadLocal.get();
        if (connect == null) {
            connect = DruidUtils.getInstance().getConnection();
            connectionThreadLocal.set(connect);
        }
        return connect;
    }


}
